package fel.via.cz.thewolfofwallstreet.components;

import android.app.Activity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import fel.via.cz.thewolfofwallstreet.LoginActivity;
import fel.via.cz.thewolfofwallstreet.MainActivity;
import fel.via.cz.thewolfofwallstreet.R;

public class ShareNewsView {

    private Activity activity;
    private ListView listView;
    private List<String> list;
    private ArrayAdapter<String> adapter;
    private String companyName;

    public ShareNewsView(Activity activity) {
        this.activity = activity;
        this.listView = (ListView) activity.findViewById(R.id.lvNews);
        this.initialize();
    }

    private void initialize() {
        this.list = new ArrayList<String>();
        this.adapter = new ArrayAdapter<String>(this.activity, android.R.layout.simple_list_item_1, list);
        this.listView.setAdapter(this.adapter);
    }

    public void setup(String companyName) {
        this.companyName = companyName;
        httpRequest();
    }

    private void httpRequest() {
        final String url = MainActivity.serverAddress + "wolfofwallstreet/news/" + companyName;

        JsonArrayRequest getRequest = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        // display response
                        Log.d("Response", response.toString());
                        setNews(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Error.Response", error.toString());
                    }
                }
        ) {
            @Override
            public HashMap<String, String> getParams() {
                HashMap<String, String> params = new HashMap<String, String>();
                params.put("Accept", "application/json");
                return params;
            }
        };
        getRequest.setRetryPolicy(new DefaultRetryPolicy(
                60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        LoginActivity.requestQueue.add(getRequest);
    }

    private void setNews(JSONArray news) {
        if(news.length() == 0) return;
        this.list.clear();
        for (int i = 0; i < news.length(); i++) {
            try {
                JSONObject jo = news.getJSONObject(i);
                this.list.add(jo.getString("date") + "\n" + jo.getString("text"));
            } catch (JSONException e) {
                Log.e("JSONObject error: ", e.toString());
            }
        }
        this.adapter.notifyDataSetChanged();
    }
}
